#include "printercontrol.h"

PrinterControl *PrinterControl::m_instance = nullptr;
QMutex PrinterControl::m_mutex;

PrinterControl::PrinterControl(QObject *parent)
    : QObject{parent}
    , m_CurrentLanguage(1)
    , m_PrinterType(1)
{
    m_Printer = new QSerialPort(this);
}

void PrinterControl::recv_Auto_Printer_Message_Slots(bool left,
                                                     bool right,
                                                     QString materialHeight,
                                                     QString materialLength)
{
    QString print_Left = left ? "90" : "45";
    QString print_Right = right ? "90" : "45";

    printerActive(materialHeight, materialLength, print_Left, print_Right);
}

PrinterControl *PrinterControl::getInstance(QObject *parent)
{
    // 双重检查锁定，保证线程安全且高效
    if (!m_instance) {
        QMutexLocker locker(&m_mutex); // 自动加锁/解锁，避免死锁
        if (!m_instance) {
            m_instance = new PrinterControl(parent);
        }
    }
    return m_instance;
}

void PrinterControl::setConnectParameter(
    QString serialPort, QString baudRate, QString dataBit, QString parityBit, QString stopBit)
{
    QSerialPort::BaudRate set_BaudRate;
    QSerialPort::DataBits set_DataBit;
    QSerialPort::Parity set_ParityBit;
    QSerialPort::StopBits set_StopBit;

    // 设置波特率
    if ("115200" == baudRate) {
        set_BaudRate = QSerialPort::Baud115200;
    } else if ("57600" == baudRate) {
        set_BaudRate = QSerialPort::Baud57600;
    } else if ("38400" == baudRate) {
        set_BaudRate = QSerialPort::Baud38400;
    } else if ("19200" == baudRate) {
        set_BaudRate = QSerialPort::Baud19200;
    } else if ("9600" == baudRate) {
        set_BaudRate = QSerialPort::Baud9600;
    } else if ("4800" == baudRate) {
        set_BaudRate = QSerialPort::Baud4800;
    } else if ("2400" == baudRate) {
        set_BaudRate = QSerialPort::Baud2400;
    } else {
        set_BaudRate = QSerialPort::Baud1200;
    }

    // 设置数据位
    if ("8" == dataBit) {
        set_DataBit = QSerialPort::Data8;
    } else if ("7" == dataBit) {
        set_DataBit = QSerialPort::Data7;
    } else if ("6" == dataBit) {
        set_DataBit = QSerialPort::Data6;
    } else {
        set_DataBit = QSerialPort::Data5;
    }

    // 设置 - 校验位
    if ("None" == parityBit || "N" == parityBit) {
        set_ParityBit = QSerialPort::NoParity;
    } else if ("Even" == parityBit || "E" == parityBit) {
        set_ParityBit = QSerialPort::EvenParity;
    } else if ("Odd" == parityBit || "O" == parityBit) {
        set_ParityBit = QSerialPort::OddParity;
    } else if ("Mark" == parityBit || "M" == parityBit) {
        set_ParityBit = QSerialPort::MarkParity;
    } else {
        set_ParityBit = QSerialPort::SpaceParity;
    }

    // 设置停止位
    if ("1" == stopBit) {
        set_StopBit = QSerialPort::OneStop;
    } else if ("1.5" == stopBit) {
        set_StopBit = QSerialPort::OneAndHalfStop;
    } else {
        set_StopBit = QSerialPort::TwoStop;
    }

    m_Printer->setPortName(serialPort);
    m_Printer->setBaudRate(set_BaudRate);
    m_Printer->setDataBits(set_DataBit);
    m_Printer->setParity(set_ParityBit);
    m_Printer->setStopBits(set_StopBit);
    // 显式设置流控制（默认无，增加可读性）
    m_Printer->setFlowControl(QSerialPort::NoFlowControl);
}

int PrinterControl::PrinterType() const
{
    return m_PrinterType;
}

void PrinterControl::setPrinterType(int newPrinterType)
{
    if (m_PrinterType == newPrinterType)
        return;
    m_PrinterType = newPrinterType;
    emit PrinterTypeChanged();
}

int PrinterControl::CurrentLanguage() const
{
    return m_CurrentLanguage;
}

void PrinterControl::setCurrentLanguage(int newCurrentLanguage)
{
    if (m_CurrentLanguage == newCurrentLanguage)
        return;
    m_CurrentLanguage = newCurrentLanguage;
    emit CurrentLanguageChanged();
}

PrinterControl::~PrinterControl() {}

void PrinterControl::printerActive(QString materialHeight,
                                   QString materialLength,
                                   QString leftAngle,
                                   QString rightAngle)
{
    switch (PrinterType()) {
    case 1: {
        if (CurrentLanguage() == 1) {
            printer_Active_RuiGong_Zh(materialHeight,
                                      materialLength,
                                      leftAngle,
                                      rightAngle,
                                      MaterialName(),
                                      CustomerName());
        } else {
            printer_Active_RuiGong_En(materialHeight,
                                      materialLength,
                                      leftAngle,
                                      rightAngle,
                                      MaterialName(),
                                      CustomerName());
        }
        break;
    }
    case 2: {
        if (CurrentLanguage() == 1) {
            printer_Active_WeiHuang_Zh(materialHeight,
                                       materialLength,
                                       leftAngle,
                                       rightAngle,
                                       MaterialName(),
                                       CustomerName());
        } else {
            printer_Active_WeiHuang_En(materialHeight,
                                       materialLength,
                                       leftAngle,
                                       rightAngle,
                                       MaterialName(),
                                       CustomerName());
        }
        break;
    }
    case 3: {
        if (CurrentLanguage() == 1) {
            printer_Active_DaPu_Zh(materialHeight,
                                   materialLength,
                                   leftAngle,
                                   rightAngle,
                                   MaterialName(),
                                   CustomerName());
        } else {
            printer_Active_DaPu_En(materialHeight,
                                   materialLength,
                                   leftAngle,
                                   rightAngle,
                                   MaterialName(),
                                   CustomerName());
        }
        break;
    }
    default: {
        break;
    }
    }
}

void PrinterControl::printer_Active_RuiGong_Zh(QString MaterialHeight,
                                               QString MaterialLength,
                                               QString LeftAngle,
                                               QString RightAngle,
                                               QString MaterialName,
                                               QString Customer)
{
    QByteArray printResult;

    QString printerMechineSet = QString::fromLocal8Bit("1B401B63001B732B40");

    QString recvParameter = QString("%1,%2,%3,%4")
                                .arg(MaterialLength)
                                .arg(MaterialHeight)
                                .arg(LeftAngle)
                                .arg(RightAngle);

    QString handleParameter = recvParameter.toUtf8().toHex();
    int parameterLength = QString::number(handleParameter.size() / 2).toInt();
    QString QR_ParameterLength = QString("%1").arg(parameterLength, 2, 16, QLatin1Char('0'));

    QString QR_print
        = QString::fromLocal8Bit("1D6CDC0032001D57041D6E200601%1%2").arg(handleParameter).arg("00");

    QString printWordSet = QString::fromLocal8Bit("1B2400001B57010D");
    QString printMethod = QString::fromLocal8Bit("1B69");

    QString recvQmlmaterialLength = QString("长度:%1\n").arg(MaterialLength);
    QString recvQmlmaterialHeight = QString("高度:%1\n").arg(MaterialHeight);
    QString recvQmlLeftAngle = QString("左角度:%1\n").arg(LeftAngle);
    QString recvQmlRightAngle = QString("右角度:%1\n").arg(RightAngle);
    QString recvQmlCustomName = QString("客户名称:\n%1\n").arg(Customer);
    QString recvQmlMaterialName = QString("型材名称:\n%1\n").arg(MaterialName);

    QString handlmaterialLength = recvQmlmaterialLength.toLocal8Bit().toHex();
    QString handlematerialHeight = recvQmlmaterialHeight.toLocal8Bit().toHex();
    QString handleLeftAngle = recvQmlLeftAngle.toLocal8Bit().toHex();
    QString handleRightAngle = recvQmlRightAngle.toLocal8Bit().toHex();
    QString handleCustomName = recvQmlCustomName.toLocal8Bit().toHex();
    QString handleMaterialName = recvQmlMaterialName.toLocal8Bit().toHex();

    printResult = printerMechineSet.toUtf8() + QR_print.toUtf8() + printWordSet.toUtf8()
                  + handlmaterialLength.toUtf8() + handlematerialHeight.toUtf8()
                  + handleLeftAngle.toUtf8() + handleRightAngle.toUtf8() + handleCustomName.toUtf8()
                  + handleMaterialName.toUtf8() + printMethod.toUtf8();

    QByteArray sendMasage = QByteArray::fromHex(printResult);

    if (m_Printer->write(sendMasage)) {
        qDebug() << "瑞工打印机 - 发送成功";
    } else {
        qDebug() << "瑞工打印机 - 发送失败";
    }
}

void PrinterControl::printer_Active_RuiGong_En(QString MaterialHeight,
                                               QString MaterialLength,
                                               QString LeftAngle,
                                               QString RightAngle,
                                               QString MaterialName,
                                               QString Customer)
{
    QByteArray printResult;

    QString printerMechineSet = QString::fromLocal8Bit("1B401B63001B732B40");

    QString recvParameter = QString("%1,%2,%3,%4")
                                .arg(MaterialLength)
                                .arg(MaterialHeight)
                                .arg(LeftAngle)
                                .arg(RightAngle);

    QString handleParameter = recvParameter.toUtf8().toHex();
    int parameterLength = QString::number(handleParameter.size() / 2).toInt();
    QString QR_ParameterLength = QString("%1").arg(parameterLength, 2, 16, QLatin1Char('0'));

    QString QR_print
        = QString::fromLocal8Bit("1D6CDC0032001D57041D6E200601%1%2").arg(handleParameter).arg("00");

    QString printWordSet = QString::fromLocal8Bit("1B2400001B57010D");
    QString printMethod = QString::fromLocal8Bit("1B69");

    QString recvQmlmaterialLength = QString("Length:%1\n").arg(MaterialLength);
    QString recvQmlmaterialHeight = QString("Height:%1\n").arg(MaterialHeight);
    QString recvQmlLeftAngle = QString("L_Right:%1\n").arg(LeftAngle);
    QString recvQmlRightAngle = QString("R_Right:%1\n").arg(RightAngle);
    QString recvQmlCustomName = QString("Custom_Name:\n%1\n").arg(Customer);
    QString recvQmlMaterialName = QString("Material_Name:\n%1\n").arg(MaterialName);

    QString handlmaterialLength = recvQmlmaterialLength.toLocal8Bit().toHex();
    QString handlematerialHeight = recvQmlmaterialHeight.toLocal8Bit().toHex();
    QString handleLeftAngle = recvQmlLeftAngle.toLocal8Bit().toHex();
    QString handleRightAngle = recvQmlRightAngle.toLocal8Bit().toHex();
    QString handleCustomName = recvQmlCustomName.toLocal8Bit().toHex();
    QString handleMaterialName = recvQmlMaterialName.toLocal8Bit().toHex();

    printResult = printerMechineSet.toUtf8() + QR_print.toUtf8() + printWordSet.toUtf8()
                  + handlmaterialLength.toUtf8() + handlematerialHeight.toUtf8()
                  + handleLeftAngle.toUtf8() + handleRightAngle.toUtf8() + handleCustomName.toUtf8()
                  + handleMaterialName.toUtf8() + printMethod.toUtf8();

    qDebug() << "发送内容: " << printResult;

    QByteArray sendMasage = QByteArray::fromHex(printResult);

    if (m_Printer->write(sendMasage)) {
        qDebug() << "瑞工打印机_en_US - 发送成功";
    } else {
        qDebug() << "瑞工打印机_en_US - 发送失败";
    }
}

void PrinterControl::printer_Active_WeiHuang_Zh(QString MaterialHeight,
                                                QString MaterialLength,
                                                QString LeftAngle,
                                                QString RightAngle,
                                                QString MaterialName,
                                                QString Customer)
{
    QByteArray printResult;

    QString printWordSet = QString::fromLocal8Bit("1B401B6330021B4C1B54001B57000000008001640118");
    QString printMethod = QString::fromLocal8Bit("0C");

    QString printWord01Set = QString::fromLocal8Bit("1B2400001D240000");
    QString printWord02Set = QString::fromLocal8Bit("1B2400001D241E00");
    QString printWord03Set = QString::fromLocal8Bit("1B2400001D243C00");
    QString printWord04Set = QString::fromLocal8Bit("1B2400001D245A00");
    QString printWord05Set = QString::fromLocal8Bit("1B2400001D247800");
    QString printWord06Set = QString::fromLocal8Bit("1B2400001D249600");
    QString printWord07Set = QString::fromLocal8Bit("1B2400001D24B400");
    QString printWord08Set = QString::fromLocal8Bit("1B2400001D24D200");

    QString recvQmlLength = QString("长度:%1\n").arg(MaterialLength);
    QString recvQmlHeight = QString("高度:%1\n").arg(MaterialHeight);
    QString recvQmlLeftAngle = QString("左角度:%1\n").arg(LeftAngle);
    QString recvQmlRightAngle = QString("右角度:%1\n").arg(RightAngle);
    QString recvQmlCustomName = QString("%1\n").arg(MaterialName);
    QString recvQmlMaterialName = QString("%1\n").arg(Customer);

    QString handleLength = recvQmlLength.toLocal8Bit().toHex();
    QString handleHeight = recvQmlHeight.toLocal8Bit().toHex();
    QString handleLeftAngle = recvQmlLeftAngle.toLocal8Bit().toHex();
    QString handleRightAngle = recvQmlRightAngle.toLocal8Bit().toHex();
    QString costomTitle = QString("客户名称:").toLocal8Bit().toHex();
    QString materialTitle = QString("型材名称:").toLocal8Bit().toHex();
    QString handleCustomName = recvQmlCustomName.toLocal8Bit().toHex();
    QString handleMaterialName = recvQmlMaterialName.toLocal8Bit().toHex();

    QString recvParameter = QString("%1,%2,%3,%4")
                                .arg(MaterialLength)
                                .arg(MaterialHeight)
                                .arg(LeftAngle)
                                .arg(RightAngle);

    QString handleParameter = recvParameter.toUtf8().toHex();
    int parameterLength = QString::number(handleParameter.size() / 2).toInt();
    QString QR_ParameterLength = QString("%1").arg(parameterLength, 2, 16, QLatin1Char('0'));
    QString printerQRCodeSet
        = QString::fromLocal8Bit("1B24C8001D2400001B5A000005%1%2").arg(QR_ParameterLength).arg("00");

    printResult = printWordSet.toUtf8() + printWord01Set.toUtf8() + handleLength.toUtf8()
                  + printWord02Set.toUtf8() + handleHeight.toUtf8() + printWord03Set.toUtf8()
                  + handleLeftAngle.toUtf8() + printWord04Set.toUtf8() + handleRightAngle.toUtf8()
                  + printWord05Set.toUtf8() + costomTitle.toUtf8() + printWord06Set.toUtf8()
                  + handleCustomName.toUtf8() + printWord07Set.toUtf8() + materialTitle.toUtf8()
                  + printWord08Set.toUtf8() + handleMaterialName.toUtf8()
                  + printerQRCodeSet.toUtf8() + handleParameter.toUtf8() + printMethod.toUtf8();

    QByteArray sendMasage = QByteArray::fromHex(printResult);

    if (m_Printer->write(sendMasage)) {
        qDebug() << "炜煌打印机 - 发送成功";
    } else {
        qDebug() << "炜煌打印机 - 发送失败";
    }
}

void PrinterControl::printer_Active_WeiHuang_En(QString MaterialHeight,
                                                QString MaterialLength,
                                                QString LeftAngle,
                                                QString RightAngle,
                                                QString MaterialName,
                                                QString Customer)
{
    QByteArray printResult;

    QString printWordSet = QString::fromLocal8Bit("1B401B6330021B4C1B54001B57000000008001640118");
    QString printMethod = QString::fromLocal8Bit("0C");

    QString printWord01Set = QString::fromLocal8Bit("1B2400001D240000");
    QString printWord02Set = QString::fromLocal8Bit("1B2400001D241E00");
    QString printWord03Set = QString::fromLocal8Bit("1B2400001D243C00");
    QString printWord04Set = QString::fromLocal8Bit("1B2400001D245A00");
    QString printWord05Set = QString::fromLocal8Bit("1B2400001D247800");
    QString printWord06Set = QString::fromLocal8Bit("1B2400001D249600");
    QString printWord07Set = QString::fromLocal8Bit("1B2400001D24B400");
    QString printWord08Set = QString::fromLocal8Bit("1B2400001D24D200");

    QString recvQmlLength = QString("Length:%1\n").arg(MaterialLength);
    QString recvQmlHeight = QString("Height:%1\n").arg(MaterialHeight);
    QString recvQmlLeftAngle = QString("L_Angle:%1\n").arg(LeftAngle);
    QString recvQmlRightAngle = QString("R_Angle:%1\n").arg(RightAngle);
    QString recvQmlCustomName = QString("%1\n").arg(MaterialName);
    QString recvQmlMaterialName = QString("%1\n").arg(Customer);

    QString handleLength = recvQmlLength.toLocal8Bit().toHex();
    QString handleHeight = recvQmlHeight.toLocal8Bit().toHex();
    QString handleLeftAngle = recvQmlLeftAngle.toLocal8Bit().toHex();
    QString handleRightAngle = recvQmlRightAngle.toLocal8Bit().toHex();
    QString costomTitle = QString("Custom_Name:").toLocal8Bit().toHex();
    QString materialTitle = QString("Material_Name:").toLocal8Bit().toHex();
    QString handleCustomName = recvQmlCustomName.toLocal8Bit().toHex();
    QString handleMaterialName = recvQmlMaterialName.toLocal8Bit().toHex();

    QString recvParameter = QString("%1,%2,%3,%4")
                                .arg(MaterialLength)
                                .arg(MaterialHeight)
                                .arg(LeftAngle)
                                .arg(RightAngle);

    QString handleParameter = recvParameter.toUtf8().toHex();
    int parameterLength = QString::number(handleParameter.size() / 2).toInt();
    QString QR_ParameterLength = QString("%1").arg(parameterLength, 2, 16, QLatin1Char('0'));
    QString printerQRCodeSet
        = QString::fromLocal8Bit("1B24C8001D2400001B5A000005%1%2").arg(QR_ParameterLength).arg("00");

    printResult = printWordSet.toUtf8() + printWord01Set.toUtf8() + handleLength.toUtf8()
                  + printWord02Set.toUtf8() + handleHeight.toUtf8() + printWord03Set.toUtf8()
                  + handleLeftAngle.toUtf8() + printWord04Set.toUtf8() + handleRightAngle.toUtf8()
                  + printWord05Set.toUtf8() + costomTitle.toUtf8() + printWord06Set.toUtf8()
                  + handleCustomName.toUtf8() + printWord07Set.toUtf8() + materialTitle.toUtf8()
                  + printWord08Set.toUtf8() + handleMaterialName.toUtf8()
                  + printerQRCodeSet.toUtf8() + handleParameter.toUtf8() + printMethod.toUtf8();

    QByteArray sendMasage = QByteArray::fromHex(printResult);

    if (m_Printer->write(sendMasage)) {
        qDebug() << "炜煌打印机_en_US - 发送成功";
    } else {
        qDebug() << "炜煌打印机_en_US - 发送失败";
    }
}

void PrinterControl::printer_Active_DaPu_Zh(QString MaterialHeight,
                                            QString MaterialLength,
                                            QString LeftAngle,
                                            QString RightAngle,
                                            QString MaterialName,
                                            QString Customer)
{
    QByteArray printResult;

    QByteArray printerInit
        = (QString("%1%2%3%4").arg("1A5B0100000000").arg("B801").arg("0001").arg("00")).toLocal8Bit();

    QByteArray MaterialHeight_Out = (QString("1A54010000050000600011%1%2%3")
                                         .arg("D0CDB2C4B8DFB6C83A")
                                         .arg(MaterialHeight.toLocal8Bit().toHex())
                                         .arg("00"))
                                        .toLocal8Bit();
    QByteArray MaterialLength_Out = (QString("1A54010000230000600011%1%2%3")
                                         .arg("D0CDB2C4B3A4B6C83A")
                                         .arg(MaterialLength.toLocal8Bit().toHex())
                                         .arg("00"))
                                        .toLocal8Bit();
    QByteArray LeftAngle_Out = (QString("1A54010000410000600011%1%2%3")
                                    .arg("D7F3BDC7B6C83A")
                                    .arg(LeftAngle.toLocal8Bit().toHex())
                                    .arg("00"))
                                   .toLocal8Bit();
    QByteArray RightAngle_Out = (QString("1A540100005F0000600011%1%2%3")
                                     .arg("D3D2BDC7B6C83A")
                                     .arg(RightAngle.toLocal8Bit().toHex())
                                     .arg("00"))
                                    .toLocal8Bit();
    QByteArray MaterialName_Out1
        = (QString("1A540100007D0000600011%1%2").arg("D0CDB2C4C3FBB3C63A").arg("00")).toLocal8Bit();
    QByteArray MaterialName_Out2
        = (QString("1A540100009B0000600011%1%2").arg(MaterialName.toLocal8Bit().toHex()).arg("00"))
              .toLocal8Bit();
    QByteArray Customer_Out1
        = (QString("1A54010000B90000600011%1%2").arg("BFCDBBA7C3FBB3C63A").arg("00")).toLocal8Bit();
    QByteArray Customer_Out2
        = (QString("1A54010000D70000600011%1%2").arg(Customer.toLocal8Bit().toHex()).arg("00"))
              .toLocal8Bit();

    QByteArray QR_Code_Init = (QString("%1%2%3%4%5%6%7")
                                   .arg("1A3100")
                                   .arg("04")
                                   .arg("04")
                                   .arg("C800")
                                   .arg("4000")
                                   .arg("05")
                                   .arg("00"))
                                  .toLocal8Bit();
    QByteArray QR_Code_Data = (QString("%1%2%3%4%5%6%7%8")
                                   .arg(MaterialHeight.toLocal8Bit().toHex())
                                   .arg("2C")
                                   .arg(MaterialLength.toLocal8Bit().toHex())
                                   .arg("2C")
                                   .arg(LeftAngle.toLocal8Bit().toHex())
                                   .arg("2C")
                                   .arg(RightAngle.toLocal8Bit().toHex())
                                   .arg("00"))
                                  .toLocal8Bit();

    QByteArray print = (QString("%1").arg("1A5D001A4F00")).toLocal8Bit();

    printResult = printerInit + MaterialHeight_Out + MaterialLength_Out + LeftAngle_Out
                  + RightAngle_Out + MaterialName_Out1 + MaterialName_Out2 + Customer_Out1
                  + Customer_Out2 + QR_Code_Init + QR_Code_Data + print;

    QByteArray sendMasage = QByteArray::fromHex(printResult);

    if (m_Printer->write(sendMasage)) {
        qDebug() << "达普打印机 - 中文 - 发送成功";
    } else {
        qDebug() << "达普打印机 - 中文 - 发送失败";
    }
}

void PrinterControl::printer_Active_DaPu_En(QString MaterialHeight,
                                            QString MaterialLength,
                                            QString LeftAngle,
                                            QString RightAngle,
                                            QString MaterialName,
                                            QString Customer)
{
    QByteArray printResult;

    QByteArray printerInit
        = (QString("%1%2%3%4").arg("1A5B0100000000").arg("B801").arg("0001").arg("00")).toLocal8Bit();

    QByteArray MaterialHeight_Out = (QString("1A54010000050000600011%1%2%3")
                                         .arg("4D6174657269616C5F4865696768743A")
                                         .arg(MaterialHeight.toLocal8Bit().toHex())
                                         .arg("00"))
                                        .toLocal8Bit();
    QByteArray MaterialLength_Out = (QString("1A54010000230000600011%1%2%3")
                                         .arg("4D6174657269616C5F4C656E6774683A")
                                         .arg(MaterialLength.toLocal8Bit().toHex())
                                         .arg("00"))
                                        .toLocal8Bit();
    QByteArray LeftAngle_Out = (QString("1A54010000410000600011%1%2%3")
                                    .arg("4C656674416E676C653A")
                                    .arg(LeftAngle.toLocal8Bit().toHex())
                                    .arg("00"))
                                   .toLocal8Bit();
    QByteArray RightAngle_Out = (QString("1A540100005F0000600011%1%2%3")
                                     .arg("5269676874416E676C653A")
                                     .arg(RightAngle.toLocal8Bit().toHex())
                                     .arg("00"))
                                    .toLocal8Bit();
    QByteArray MaterialName_Out1
        = (QString("1A540100007D0000600011%1%2").arg("4D6174657269616C5F4E616D653A").arg("00"))
              .toLocal8Bit();
    QByteArray MaterialName_Out2
        = (QString("1A540100009B0000600011%1%2").arg(MaterialName.toLocal8Bit().toHex()).arg("00"))
              .toLocal8Bit();
    QByteArray Customer_Out1
        = (QString("1A54010000B90000600011%1%2").arg("437573746F6D5F4E616D653A").arg("00"))
              .toLocal8Bit();
    QByteArray Customer_Out2
        = (QString("1A54010000D70000600011%1%2").arg(Customer.toLocal8Bit().toHex()).arg("00"))
              .toLocal8Bit();

    QByteArray QR_Code_Init = (QString("%1%2%3%4%5%6%7")
                                   .arg("1A3100")
                                   .arg("04")
                                   .arg("04")
                                   .arg("C800")
                                   .arg("4000")
                                   .arg("05")
                                   .arg("00"))
                                  .toLocal8Bit();
    QByteArray QR_Code_Data = (QString("%1%2%3%4%5%6%7%8")
                                   .arg(MaterialHeight.toLocal8Bit().toHex())
                                   .arg("2C")
                                   .arg(MaterialLength.toLocal8Bit().toHex())
                                   .arg("2C")
                                   .arg(LeftAngle.toLocal8Bit().toHex())
                                   .arg("2C")
                                   .arg(RightAngle.toLocal8Bit().toHex())
                                   .arg("00"))
                                  .toLocal8Bit();

    QByteArray print = (QString("%1").arg("1A5D001A4F00")).toLocal8Bit();

    printResult = printerInit + MaterialHeight_Out + MaterialLength_Out + LeftAngle_Out
                  + RightAngle_Out + MaterialName_Out1 + MaterialName_Out2 + Customer_Out1
                  + Customer_Out2 + QR_Code_Init + QR_Code_Data + print;

    QByteArray sendMasage = QByteArray::fromHex(printResult);

    if (m_Printer->write(sendMasage)) {
        qDebug() << "达普打印机_en_US - 发送成功";
    } else {
        qDebug() << "达普打印机_en_US - 发送失败";
    }
}

void PrinterControl::connectToDevice()
{
    // 先关闭已打开的连接，避免重复连接
    if (m_Printer->isOpen()) {
        m_Printer->close();
    }

    if (m_Printer->open(QIODevice::ReadWrite)) {
        qDebug() << "Cpp: 打印机 连接成功，端口：" << m_Printer->portName();
    } else {
        qDebug() << "Cpp: 打印机 连接失败，错误：" << m_Printer->errorString();
    }
}

QString PrinterControl::MaterialName() const
{
    return m_MaterialName;
}

void PrinterControl::setMaterialName(const QString &newMaterialName)
{
    if (m_MaterialName == newMaterialName)
        return;
    m_MaterialName = newMaterialName;
    emit MaterialNameChanged();
}

QString PrinterControl::CustomerName() const
{
    return m_CustomerName;
}

void PrinterControl::setCustomerName(const QString &newCustomerName)
{
    if (m_CustomerName == newCustomerName)
        return;
    m_CustomerName = newCustomerName;
    emit CustomerNameChanged();
}
